//
//  PolicySearchViewController.m
//  阿帕
//
//  Created by mac on 2020/1/6.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "PolicySearchViewController.h"

@interface PolicySearchViewController ()
{
    NSString *_startDate;
    NSString *_endDate;
}
@end

@implementation PolicySearchViewController

static const CGFloat itemHeight = 45;

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _startDate = @"";
    _endDate = @"";
    
    self.navigationItem.title = @"保单搜索";
    
    self.view.backgroundColor = BackgroundColor;
    
    [self setupBaseView];
}

- (void)setupBaseView {
    
    NSArray *formsTitleArr = @[@"货源单号",@"运输单号",@"保单号",@"开始时间",@"截止时间",@"货主信息", @"司机信息"];
    NSArray *placeholderArr = @[@"请填写货源单号",@"请填写运输单号",@"请填写保单号",@"请选择开始时间",@"请选择截止时间",@"姓名/电话",@"姓名/电话"];
    
    for (int i=0; i<formsTitleArr.count; i++) {
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 1 + itemHeight*i, ScreenW, itemHeight)];
        bgView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:bgView];
        
        //输入框
        if (i==3 || i==4) {
            //按钮
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(ScreenW-230, 0, 200, itemHeight);
            button.tag = 10 + i;
            [button setTitle:placeholderArr[i] forState:UIControlStateNormal];
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            button.titleLabel.font = font(14);
            [button setTitleColor:Light_TextColor forState:UIControlStateNormal];
            [button addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
            [bgView addSubview:button];
            
            UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenW-20, 17, 6, 11)];
            arrow.image = image(@"gcy_icon_hd_rarrow");
            [bgView addSubview:arrow];
            
        } else {
            
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenW-215, 0, 200, itemHeight)];
            textField.tag = 10 + i;
            textField.font = font(14);
            textField.text = @"";
            textField.textColor = Dark_TextColor;
            textField.textAlignment = NSTextAlignmentRight;
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderArr[i] attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
            [bgView addSubview:textField];
        }
        
        //标题
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 120, itemHeight)];
        textLabel.text = formsTitleArr[i];
        textLabel.textColor = Dark_TextColor;
        textLabel.font = font(14);
        textLabel.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:textLabel];
        
        //分割线
        UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(15, itemHeight-1, ScreenW - 30, 1)];
        hLine.backgroundColor = BackgroundColor;
        [bgView addSubview:hLine];
    }
    
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searchBtn setBackgroundColor:Theme_ColorValue];
    cut(searchBtn, BTN_BORDER_RADIUS);
    searchBtn.tag = 100;
    [searchBtn addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    searchBtn.frame = CGRectMake(20, 7 * 45 + 20, ScreenW-40, 45);
    [self.view addSubview:searchBtn];
}

- (void)btnEventClick:(UIButton *)button {
    if (button.tag == 13) {
        //开始时间
        BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMDHMS];
        datePickerView.title = @"选择开始时间";
        datePickerView.isAutoSelect = NO;
        datePickerView.showToday = YES; // 是否显示“今天”
        datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
            [button setTitle:selectValue forState:UIControlStateNormal];
            _startDate = selectValue;
        };
        [datePickerView show:self];
        
    } else if (button.tag == 14) {
        //截止时间
        BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMDHMS];
        datePickerView.title = @"选择截止时间";
        datePickerView.isAutoSelect = NO;
        datePickerView.showToday = YES; // 是否显示“今天”
        datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
            [button setTitle:selectValue forState:UIControlStateNormal];
            _endDate = selectValue;
        };
        [datePickerView show:self];
        
    } else {
        //搜索
        UITextField *hydhTextField = [self.view viewWithTag:10];
        UITextField *ysdhTextField = [self.view viewWithTag:11];
        UITextField *bdhTextField = [self.view viewWithTag:12];
        UITextField *hzTextField = [self.view viewWithTag:15];
        UITextField *sjTextField = [self.view viewWithTag:16];
        
        NSDictionary *dic = @{
            @"mainOrderNumber": hydhTextField.text,//货源单号
            @"orderDetailNumber": ysdhTextField.text,//运输单号
            @"applyPolicyNo": bdhTextField.text,//保单号
            @"gmtCreatedStart": _startDate,//保单开始时间
            @"gmtCreatedEnd": _endDate,//保单截止时间
            @"shipmentInfo": hzTextField.text,//货主信息
            @"driverInfo": sjTextField.text,//司机信息
        };
        if (self.delegate && [self.delegate respondsToSelector:@selector(GetSearchConditionResult:)]) {
            [self.delegate GetSearchConditionResult:dic];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

@end
